package com.sparrow.mvc.ui;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.sparrow.core.Log;
import com.sparrow.utils.StringUtil;

@SuppressWarnings("serial")
public class JRedioBoxList extends JWebControl {
	private String col = "3";
	private boolean isShowTable = true;
	private String selectedValue;
	private String defaultSelectValue;
	private String valueText;
	private String indent = "0";

	public boolean isShowTable() {
		return isShowTable;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public void setShowTable(boolean isShowTable) {
		this.isShowTable = isShowTable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException {
		int returnValue = TagSupport.SKIP_BODY;
		StringBuffer writeHTML = new StringBuffer();
		// value test
		Map<String, String> valueTextList = null;
		// 如果后台有设置 id.valueText
		valueTextList = (LinkedHashMap<String, String>) this.pageContext
				.getRequest().getAttribute(this.getId() + ".valueText");
		// 如果后台没有设置
		if (valueTextList == null) {
			valueTextList = new LinkedHashMap<String, String>();
			String[] options;
			String[] option = new String[2];
			if (!StringUtil.isNullOrEmpty(this.valueText)) {
				options = this.valueText.split(",");
				for (int i = 0; i < options.length; i++) {
					option = options[i].split(":");
					valueTextList.put(option[0], option[1]);
				}
			}
		}
		if (this.getVisible().toLowerCase() != "false" && valueTextList != null
				&& valueTextList.size() > 0) {
			if (this.isShowTable()) {
				writeHTML.append("<table cellpadding=\"4\" cellspacing=\"0\"");
				writeHTML.append(String.format(" id=\"%1$s\"", this.getId()));
				writeHTML.append(this.getCssClass());
				writeHTML.append(this.getCssText());
				writeHTML.append(this.getTitle());
				writeHTML.append(this.getEvents());
				writeHTML.append(">");
			}
			String strValue;
			String strChecked = "";
			Set<String> value = valueTextList.keySet();
			Iterator<String> it = value.iterator();
			int row = (int) Math.ceil(valueTextList.size()
					/ Double.valueOf(col));
			for (int i = 0; i < Integer.valueOf(row); i++) {
				if (this.isShowTable()) {
					writeHTML.append(StringUtil.getIndent(Integer.valueOf(this
							.getIndent() + 1))
							+ "<tr>");
				}
				for (int j = 0; j < Integer.valueOf(col); j++) {
					if (it.hasNext()) {
						strValue = it.next();
						if (i == 0 && j == 0) {
							this.defaultSelectValue = strValue;
						}
						if (strValue.equals(this.getSelectedValue())) {
							strChecked = "checked=true";
						} else {
							strChecked = "";
						}
						if (this.isShowTable()) {
							writeHTML.append(StringUtil.getIndent(Integer
									.valueOf(this.getIndent() + 2))
									+ "<td>");
						}
						writeHTML
								.append(StringUtil.getIndent(Integer
										.valueOf(this.getIndent()) + 3)
										+ String
												.format(
														"<input id=\"rdb%1$s%2$s\"  type=\"radio\" %3$s value=\"%4$s\"  %6$s /><label for=\"rdb%1$s%2$s\" >%5$s</label>",
														// 行
														String.valueOf(i),
														// 列
														String.valueOf(j),
														// name
														this.getName(),
														// value
														strValue,
														// test
														valueTextList
																.get(strValue),
														// 是否被选中
														strChecked));
						if (this.isShowTable) {
							writeHTML.append("</td>");
						}
					} else {
						if (this.isShowTable()) {
							writeHTML.append("<td></td>");
						}
					}
				}
				if (this.isShowTable()) {
					writeHTML.append(StringUtil.getIndent(Integer.valueOf(this
							.getIndent() + 1))
							+ "</tr>");
				}
			}
			if (this.isShowTable()) {
				writeHTML.append("</table>");
			}
		}
		try {
			if (valueTextList != null && valueTextList.size() != 0) {
				this.pageContext.getOut().println(writeHTML.toString());
			}
		} catch (IOException e) {
			Log.getInstance().error(e);
		}
		return returnValue;

	}

	public void setValueText(String valueText) {
		this.valueText = valueText;
	}

	public String getCol() {
		return col;
	}

	public void setCol(String col) {
		this.col = col;
	}

	public String getIndent() {
		return indent;
	}

	public void setIndent(String indent) {
		this.indent = indent;
	}

	public String getSelectedValue() {
		String selectValue = (String) this.pageContext.getRequest()
				.getAttribute(this.getId() + ".selectedValue");
		if (selectValue == null) {
			selectValue = (String) this.pageContext.getRequest().getAttribute(
					this.getCtrlName());
		}
		if (selectValue == null) {
			selectValue = this.selectedValue;
		}
		if (StringUtil.isNullOrEmpty(selectValue)) {
			selectValue = this.defaultSelectValue;
		}
		return selectValue;
	}
}
